<html>
<head><meta charset="utf-8"><title>reading MIR output · t-compiler/wg-mir-opt · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/index.html">t-compiler/wg-mir-opt</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/reading.20MIR.20output.html">reading MIR output</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="229638599"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/reading%20MIR%20output/near/229638599" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Doc Jones <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/reading.20MIR.20output.html#229638599">(Mar 10 2021 at 11:08)</a>:</h4>
<p>I'm early in my Rust journey and trying to understand some things deeply about how Rust handles String, str and string literals.  I'm using the playground to produce MIR output from some code.  Is there a resource to help me quickly learn to decipher the MIR output?  Or perhaps someone would have some time to answer a few questions that I have?</p>



<a name="229640965"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/reading%20MIR%20output/near/229640965" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Notification Bot <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/reading.20MIR.20output.html#229640965">(Mar 10 2021 at 11:28)</a>:</h4>
<p>This topic was moved here from <a class="stream-topic" data-stream-id="182449" href="/#narrow/stream/182449-t-compiler.2Fhelp/topic/reading.20MIR.20output">#t-compiler/help &gt; reading MIR output</a> by <span class="user-mention silent" data-user-id="124288">oli</span></p>



<a name="229641132"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/reading%20MIR%20output/near/229641132" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/reading.20MIR.20output.html#229641132">(Mar 10 2021 at 11:30)</a>:</h4>
<p>Feel free to ask any questions you may have here. I don't think we have any resources on reading MIR dumps</p>



<a name="229642241"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/reading%20MIR%20output/near/229642241" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/reading.20MIR.20output.html#229642241">(Mar 10 2021 at 11:40)</a>:</h4>
<p>reading MIR dumps seems to be a pretty big hammer just to understand how strings are handled, though</p>



<a name="229645843"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/reading%20MIR%20output/near/229645843" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Doc Jones <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/reading.20MIR.20output.html#229645843">(Mar 10 2021 at 12:11)</a>:</h4>
<p><span class="user-mention silent" data-user-id="129457">Florian Diebold</span> <a href="#narrow/stream/189540-t-compiler.2Fwg-mir-opt/topic/reading.20MIR.20output/near/229642241">said</a>:</p>
<blockquote>
<p>reading MIR dumps seems to be a pretty big hammer just to understand how strings are handled, though</p>
</blockquote>
<p>Maybe, but I'm trying to understand something specific from the docs.  In the docs there is a statement that says String(s) implement Deref which causes String to inherit all of str's methods.  From the code example, it's demonstrated that a String can produce/spawn &amp;str(s) without explicitly implementing Deref.  So, I'm trying to read the MIR dump to see how this happens. Further, if a String which has the attributes: ptr, bytes(backing array) and capacity and a str has ptr and length which are descriptors of some "backing array" then what I would like to describe are the distinctions and commonalities between String and str.  And even examine the question:  Is there a str inside of String?  I know the answer to this should be "it's complicated", but it would be super helpful to read the MIR dump.  That information helps me to see where and maybe how the conversions from &amp;str to String and back again occur in this code snippet and gain insight into exactly what the compiler instructions are.  I don't know if this makes sense, but it is as I understand things.</p>



<a name="229647186"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/reading%20MIR%20output/near/229647186" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Doc Jones <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/reading.20MIR.20output.html#229647186">(Mar 10 2021 at 12:21)</a>:</h4>
<p>The MIR dump I'm trying to read in a gh gist.  But of course, I want to be able to read these anytime.<br>
<a href="https://gist.github.com/doc-jones/5cad48f106a3b1dac1da874aa92ceacb">https://gist.github.com/doc-jones/5cad48f106a3b1dac1da874aa92ceacb</a></p>



<a name="229647483"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/reading%20MIR%20output/near/229647483" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/reading.20MIR.20output.html#229647483">(Mar 10 2021 at 12:23)</a>:</h4>
<p><span class="user-mention silent" data-user-id="394977">Doc Jones</span> <a href="#narrow/stream/189540-t-compiler.2Fwg-mir-opt/topic/reading.20MIR.20output/near/229645843">said</a>:</p>
<blockquote>
<p>From the code example, it's demonstrated that a String can produce/spawn &amp;str(s) without explicitly implementing Deref.  So, I'm trying to read the MIR dump to see how this happens.</p>
</blockquote>
<p>Do you mean "without explicitly invoking <code>Deref::deref</code>"? Because it most certainly explicitly implements <code>Deref</code>: <a href="https://doc.rust-lang.org/std/string/struct.String.html#deref-methods">https://doc.rust-lang.org/std/string/struct.String.html#deref-methods</a> (source: <a href="https://doc.rust-lang.org/src/alloc/string.rs.html#2126">https://doc.rust-lang.org/src/alloc/string.rs.html#2126</a>)</p>



<a name="229649329"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/reading%20MIR%20output/near/229649329" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Doc Jones <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/reading.20MIR.20output.html#229649329">(Mar 10 2021 at 12:37)</a>:</h4>
<p>What I mean is that a developer need not explicitly implement Deref.  It doesn't need to appear in the source code, if you will.</p>
<p><a href="https://doc.rust-lang.org/std/string/struct.String.html#deref">https://doc.rust-lang.org/std/string/struct.String.html#deref</a></p>



<a name="229650678"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/reading%20MIR%20output/near/229650678" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/reading.20MIR.20output.html#229650678">(Mar 10 2021 at 12:47)</a>:</h4>
<p>You don't need to <em>call</em> <code>deref</code>, but the <code>Deref</code> trait needs to be implemented</p>



<a name="229651171"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/reading%20MIR%20output/near/229651171" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Doc Jones <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/reading.20MIR.20output.html#229651171">(Mar 10 2021 at 12:50)</a>:</h4>
<p><span class="user-mention silent" data-user-id="211727">Jonas Schievink  [he/him]</span> <a href="#narrow/stream/189540-t-compiler.2Fwg-mir-opt/topic/reading.20MIR.20output/near/229650678">said</a>:</p>
<blockquote>
<p>You don't need to <em>call</em> <code>deref</code>, but the <code>Deref</code> trait needs to be implemented</p>
</blockquote>
<p>#![allow(unused)]<br>
fn main() {<br>
fn takes_str(s: &amp;str) { }</p>
<p>let s = String::from("Hello");</p>
<p>takes_str(&amp;s);<br>
}</p>



<a name="229651476"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/reading%20MIR%20output/near/229651476" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/reading.20MIR.20output.html#229651476">(Mar 10 2021 at 12:52)</a>:</h4>
<p>The standard library contains a <code>Deref</code> implementation for <code>String</code></p>



<a name="229652178"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/reading%20MIR%20output/near/229652178" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Doc Jones <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/reading.20MIR.20output.html#229652178">(Mar 10 2021 at 12:57)</a>:</h4>
<p><span class="user-mention silent" data-user-id="211727">Jonas Schievink  [he/him]</span> <a href="#narrow/stream/189540-t-compiler.2Fwg-mir-opt/topic/reading.20MIR.20output/near/229651476">said</a>:</p>
<blockquote>
<p>The standard library contains a <code>Deref</code> implementation for <code>String</code></p>
</blockquote>
<p>I appreciate your thoughts, but I'm afraid we've veered off topic.  I'm hoping to get assistance in learning how to read MIR dumps.</p>



<a name="229659308"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/reading%20MIR%20output/near/229659308" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/reading.20MIR.20output.html#229659308">(Mar 10 2021 at 13:39)</a>:</h4>
<p>well... I think the point by Jonas is very relevant to the topic. As you can see in the MIR, there is a call to <code>deref</code> in the source, which invokes the implementation from the standard library. So yes, you do not need to <em>call</em> deref manually, but there must be an <code>impl</code> in the standard library for that to work. This is nothing special to <code>String</code>, this works for any type that anyone implements <code>Deref</code> for.</p>



<a name="229660905"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/reading%20MIR%20output/near/229660905" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Doc Jones <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/reading.20MIR.20output.html#229660905">(Mar 10 2021 at 13:48)</a>:</h4>
<p><span class="user-mention silent" data-user-id="124288">oli</span> <a href="#narrow/stream/189540-t-compiler.2Fwg-mir-opt/topic/reading.20MIR.20output/near/229659308">said</a>:</p>
<blockquote>
<p>well... I think the point by Jonas is very relevant to the topic. As you can see in the MIR, there is a call to <code>deref</code> in the source, which invokes the implementation from the standard library. So yes, you do not need to <em>call</em> deref manually, but there must be an <code>impl</code> in the standard library for that to work. This is nothing special to <code>String</code>, this works for any type that anyone implements <code>Deref</code> for.</p>
</blockquote>
<p>Thanks for the opinion, oli.  As my goal here is to ask for help in learning how to read MIR output, I probably shouldn't have taken the bait on why I needed to know something that I was asking for help to learn.  Both you and Jonas are making points that are appreciated, but not relevant to my reason for reaching out and I apologize for the tangent.  The topic of my post is reading MIR output.  I think I'll just have to be less lazy web and figure it out.  :-)  Thanks, truly, for your time.<br>
<a href="https://rustc-dev-guide.rust-lang.org/mir/index.html">https://rustc-dev-guide.rust-lang.org/mir/index.html</a></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>